<?php
/*
error_reporting(-1);
require'vendor/autoload.php';


use Endroid\QrCode\Color\Color;
use Endroid\QrCode\Encoding\Encoding;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
use Endroid\QrCode\Writer\PngWriter;
echo phpinfo();die;
$writer = new PngWriter();

// Create QR code
$qrCode = QrCode::create('test')
    ->setEncoding(new Encoding('UTF-8'))
    ->setErrorCorrectionLevel(new ErrorCorrectionLevelLow())
    ->setSize(300)
    ->setMargin(10)
    ->setRoundBlockSizeMode(new RoundBlockSizeModeMargin())
    ->setForegroundColor(new Color(0, 0, 0))
    ->setBackgroundColor(new Color(255, 255, 255));
$result = $writer->write($qrCode, $logo, $label);
header('Content-Type: '.$result->getMimeType());
echo $result->getString();

*/
//declare(strict_types =1);
//set_time_limit(200);
//ini_set('memory_limit', '128M');

//冒泡排序

function maoPao($arr)
{
    $count = count($arr);
    for ($i = 0; $i < $count - 1; $i++) {
        for ($j = 0; $j < $count - $i - 1; $j++) {
            if ($arr[$j] > $arr[$j + 1]) {
                $t = $arr[$j];
                $arr[$j] = $arr[$j + 1];
                $arr[$j + 1] = $t;
            }
        }
    }
    return $arr;
}

//选择 (每次子循环找到最大或最小值，与当前键交换位置)
function change($arr)
{
    $count = count($arr);
    for ($i = 0; $i < $count - 1; $i++) { //不需要和最后一个数比较，前面排好序了最后一个一定是排好序的
        $key = $i;
        for ($j = $i + 1; $j < $count; $j++) { //每次下一个与后面的全部进行比较，找到最大最小的值
            if ($arr[$key] > $arr[$j]) {
                $key = $j;
            }
        }
        $t = $arr[$i];
        $arr[$i] = $arr[$key];
        $arr[$key] = $t;
    }
    return $arr;
}

//插入排序
//自循环每次与前面的全部值进行比较，第一次循环不用操作，保存当前循环的值，
//与他前面的每一个值比较，只要有比他大或小的值，将后面的每个值都做前移操作，
//前移完成后将本次循环的数据放到标记的key-1中，减去自增量。
function insert1($arr)
{
    $count = count($arr);
    for ($i = 0; $i < $count; $i++) {
        $key = $i - 1; //与前面的所有数进行比较
        $value = $arr[$i];
        while ($key >= 0 && $arr[$key] > $value) {
            $arr[$key + 1] = $arr[$key];
            $key--;
        }
        $arr[$key + 1] = $value;
    }
    return $arr;
}

$arr = [1, 3, 6, 5, 4, 2, 8, 7];

//$res = insert1($arr);
//var_dump($res);
function maoPao1($arr)
{
    if (empty($arr)) {
        return false;
    }
    $count = count($arr);
    for ($i = 0; $i < $count - 1; $i++) {
        for ($j = 0; $j < $count - $i - 1; $j++) {
            if ($arr[$j] > $arr[$j + 1]) {
                $t = $arr[$j];
                $arr[$j] = $arr[$j + 1];
                $arr[$j + 1] = $t;
            }
        }
    }
    return $arr;
}

//print_r(maoPao1($arr));
function change1($arr)
{
    $count = count($arr);
    for ($i = 0; $i < $count - 1; $i++) {
        $k = $i;
        for ($j = $i + 1; $j < $count; $j++) {
            if ($arr[$k] > $arr[$j]) {
                $k = $j;
            }
        }
        $t = $arr[$i];
        $arr[$i] = $arr[$k];
        $arr[$k] = $t;
    }
    return $arr;
}

//
//$conn = new \Redis();
//$conn->connect("10.0.0.200","6379");
//$conn->auth('wyqgg');
//
//var_dump($conn->keys("*"));
//
//$conn->sIsMember();
function maoPao2($arr)
{
    if (empty($arr)) {
        return false;
    }
    $count = count($arr);
    for ($i = 0; $i < $count - 1; $i++) {
        for ($j = 0; $j < $count - $i - 1; $j++) {
            if ($arr[$j] > $arr[$j + 1]) {
                $tem = $arr[$j];
                $arr[$j] = $arr[$j + 1];
                $arr[$j + 1] = $tem;
            }
        }
    }
    return $arr;
}

function change2($arr)
{
    $count = count($arr);
    for ($i = 0; $i < $count - 1; $i++) {
        $maxKey = $i;
        for ($j = $i; $j < $count; $j++) {
            if ($arr[$j] > $arr[$maxKey]) {
                $maxKey = $j;
            }
        }
        $tem = $arr[$i];
        $arr[$i] = $arr[$i + 1];
        $arr[$i + 1] = $tem;
    }
    return $arr;
}

function insert2($arr)
{
    $count = count($arr);
    for ($i = 0; $i < $count; $i++) {
        $maxKey = $i - 1; //与前面的值作比较
        $maxValue = $arr[$i];
        while ($maxKey >= 0 && $arr[$maxKey] > $maxValue) {
            $arr[$maxKey + 1] = $arr[$maxKey];
            $maxKey--;
        }
        $arr[$maxKey + 1] = $maxValue;
    }
    return $arr;
}

function getRate($arr)
{
    if (empty($arr) || !is_array($arr)) {
        return false;
    }
    $sum = array_sum($arr);//计算总步长
    if ($sum < 100) {
        $sum = 100;
    }
    $rand = rand(1, $sum);//随机数
    $min = 1;
    foreach ($arr as $k => $v) {
        $max = $min + $v;//当前右区间
        if ($rand < $max && $rand >= $min) { //在区间内就返回
            return $k;
        }
        $min = $max;//左区间变右区间
    }
}

function quick_sort($arr)
{
//    if (!is_array($arr)) {
//        return false;
//    }
    $count = count($arr);
    if ($count <= 1) {
        return $arr;
    }
    $left = $right = [];
    for ($i = 1; $i < $count; $i++) {
        if ($arr[$i] >= $arr[0]) {
            $right[] = $arr[$i];
        } else {
            $left[] = $arr[$i];
        }
    }
    $left = quick_sort($left);
    $right = quick_sort($right);
    return array_merge($left, array($arr[0]), $right);
}

class Single
{
    public function __construct()
    {
    }
}

function getData()
{
    $arr = [];
    for ($i = 0; $i < 300000; $i++) {
        $arr[] = [
            "id" => 1,
            "sex" => $i % 2 == 0 ? "男" : "女",
            "name" => rand(1, 20),
        ];
    }
    return $arr;
}

function time424()
{
    $todayfrom = mktime(0, 0, 0, date("n"), date("j"), date("Y"));
    $weekfrom = date('D') == "Sun" ? strtotime('monday last week') : strtotime('monday this week');
    $monthfrom = mktime(0, 0, 0, date("n"), 01, date("Y"));
    $todayto = $weekto = $monthto = time();
    var_dump($todayfrom);
    var_dump($weekfrom);
    var_dump($monthfrom);
    var_dump($todayto);
}

function countTimeArr($arr)
{
    $time = microtime(true);

    for ($i = 1; $i < count($arr); $i++) {
        for ($j = 1; $j < count($arr); $j++) {
            $arr[$j + 1];
        }
    }
    echo 'execution_time===> [' . date('Y-m-d H:i:s') . '] - ' . (microtime(true) - $time) . 's Finished' . PHP_EOL;
}

function countTimeArr2($arr)
{
    $count = count($arr);
    $time = microtime(true);
    for ($i = 1; $i < $count; $i++) {
        for ($j = 1; $j < $count; $j++) {
            $arr[$j + 1];
        }
    }
    echo 'execution_time===> [' . date('Y-m-d H:i:s') . '] - ' . (microtime(true) - $time) . 's Finished' . PHP_EOL;
}


function getPdo($dbms = 'mysql', $host = "127.0.0.1", $dbName = "wyq")
{
    $dsn = "$dbms:host=$host;dbname=$dbName";
    $pdo = new PDO($dsn, 'root', 'root');
    return $pdo;
}

function dbTest()
{
    $time = microtime(true);
    $pdo = getPdo();
    for ($i = 1529; $i < 2529; $i++) {
//        $sql = "insert into `jobtest`(`init_time`) values (?)"; // SQL语句
        $sql = "update `jobtest` set `init_time` = 'upda' where id = ?";
        $stmt = $pdo->prepare($sql);
//        $res = $stmt->execute([time()]);
        $res = $stmt->execute([$i]);
    }
    echo 'execution_time===> [' . date('Y-m-d H:i:s') . '] - ' . (microtime(true) - $time) . 's Finished' . PHP_EOL;
}

function dbTest2()
{
    $time = microtime(true);
    $pdo = getPdo();
    $idStr = "";
    $arr = [];
    for ($i = 1529; $i < 2529; $i++) {
        $idStr .= $i.',';
        $arr[$i] = "update";
    }
    $idStr = substr($idStr,0,strlen($idStr)-1);
    $sql = "update `jobtest` set `init_time` = 'update' where `id` in($idStr)";
    $stmt = $pdo->prepare($sql);
    $res = $stmt->execute();
    echo 'execution_time===> [' . date('Y-m-d H:i:s') . '] - ' . (microtime(true) - $time) . 's Finished' . PHP_EOL;

}




function test()
{
    $arr = func_get_args();
    var_dump($arr);
}
//dbTest();
//dbTest2();

//test([['test1','test2']]);

function csvToArray($csvFile){



$file_to_read = fopen($csvFile, 'r');
    $res = mb_detect_encoding($csvFile);
//    var_dump($res);die;
    $str = [];
    while (!feof($file_to_read) ) {

        $lines = fgetcsv($file_to_read, 1000);
        $str[] = $lines;
    }


    fclose($file_to_read);

    return $str;

}

function tripleInteger($int)
{
    if(!is_int($int))
        throw new InvalidArgumentException('tripleInteger function only accepts integers. Input was: '.$int);
    return $int * 3;
}

//$x = tripleInteger(4); //$x == 12
//$x = tripleInteger(2.5); //exception will be thrown as 2.5 is a float
//$x = tripleInteger('foo'); //exception will be thrown as 'foo' is a string
//$x = tripleInteger('4'); //exception will throw as '4' is also a string



//$host = "127.0.0.1";
//$port = "6380";
//$conn = new Redis();
//$conn->connect($host,$port);
//$conn->select(1);
//$conn->set('name','wyq');
//$res = $conn->keys('*');
//var_dump($res);

$data = [
    'id' => 1,
    'title' => "test",
    'first_name' => "test",
    'othername' => "test",
    'sex' => "man",
];

var_dump(array_search('othername',));